package com.ecp.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.ecp.commons.Global;

public class HttpRequestUtil {

	private static final int	PORT			= 80;
	private static HttpClient	client;
	private static final String	CONTENT_CHARSET	= "UTF-8";

	public HttpRequestUtil() {
		client = new HttpClient();
		client.getHostConfiguration().setHost(Global.SERVER_ADDR, PORT, "http");
		client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
		client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
		client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);
	}

	public String doRequestByGet(String uri) {
		try {
			GetMethod gm = new GetMethod(uri);
			client.executeMethod(gm);
			BufferedReader br = new BufferedReader(new InputStreamReader(gm.getResponseBodyAsStream()));
			StringBuffer stringBuffer = new StringBuffer();
			String str = "";
			while ((str = br.readLine()) != null) {
				stringBuffer.append(str);
			}
			String content = stringBuffer.toString();
			gm.releaseConnection();
			return content;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public String doRequestByPost(String uri, String tel, String content) {
		try{
			PostMethod pm = new PostMethod(uri);
			NameValuePair[] data = { new NameValuePair("sname", "bjymt"), new NameValuePair("spwd", "111111"),
					new NameValuePair("sphones", tel), new NameValuePair("smsg", new String(content.getBytes(),"utf-8")) };
			pm.setRequestBody(data);
			client.executeMethod(pm);
			BufferedReader br = new BufferedReader(new InputStreamReader(pm.getResponseBodyAsStream()));
			StringBuffer stringBuffer = new StringBuffer();
			String str = "";
			while ((str = br.readLine()) != null) {
				stringBuffer.append(str);
			}
			String code = stringBuffer.toString();
			pm.releaseConnection();
			return code;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static String SMS(String postData, String postUrl) {
        try {
            //发送POST请求
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(postData);
           // System.out.println(postData);
            out.flush();
            out.close();

            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }

}
